﻿
Partial Class WebForm_ManageProduct_ManageMaintainDummy
    Inherits System.Web.UI.UserControl

    Dim pmanager As New ProductManager

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack() Then
            Me.BindDDLDivision()
            Me.ReBindData("Signature")
            Me.BindDDLMMPP(Me.ddlMMPP)
        End If
    End Sub

#Region "Gridview事件"

#Region "DummyHierarchy"
    Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If Me.gv1.DataKeys(e.Row.RowIndex).Item("IsDummy").ToString() = "N" OrElse Me.gv1.DataKeys(e.Row.RowIndex).Item("CreateBySystem").ToString() = "Y" Then
                TryCast(e.Row.FindControl("btnEdit"), Button).Visible = False
                TryCast(e.Row.FindControl("btnDelete"), Button).Visible = False
            End If
        End If
    End Sub

    Protected Sub gv1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv1.PageIndexChanging
        Me.gv1.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv1.RowCommand
        If e.CommandName = "EditDummyHierarchy" Then
            Me.AccessMode = "Update"
            Me.SetLevel()

            Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
            Me.gv1.SelectedIndex = row.RowIndex
            Me.SelectedID = Me.gv1.DataKeys(row.RowIndex).Item("ID").ToString()
            Me.txtProductHierarchy.Enabled = False
            Me.txtProductHierarchy.Text = Right(Me.gv1.DataKeys(row.RowIndex).Item("Code").ToString(), 2)
            Me.txtDescription.Text = Me.gv1.DataKeys(row.RowIndex).Item("Description").ToString()

            Me.mvEmployee.SetActiveView(vwAddDummyHierarchy)
        ElseIf e.CommandName = "DeleteDummyHierarchy" Then
            Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
            Dim sql As New MyStringBuilder
            Dim dat As New clsData

            sql.Length = 0
            sql.AppendFormat("Update ProductHierarchyOriginal set Active='N' ")
            sql.AppendFormat("where Left(Code,Len('{0}'))='{0}'", Me.gv1.DataKeys(row.RowIndex).Item("Code").ToString())

            '刪除dummy hierarchy後，能保留dummy sku的值，而狀態仍為Y，再讓User透過in-active按鈕自行刪除
            '==========================================================================================
            'sql.AppendFormat(";Delete from SFReport{0} ", DateTime.Today.ToString("yyyyMM"))
            'sql.AppendFormat("where SapCode in(select SapCode from ProductData where Left(ProdHierarchy,Len('{0}'))='{0}') ", Me.gv1.DataKeys(row.RowIndex).Item("Code").ToString())
            'sql.AppendFormat(";Delete from SO_SFReport{0} ", DateTime.Today.ToString("yyyyMM"))
            'sql.AppendFormat("where SapCode in(select SapCode from ProductData where Left(ProdHierarchy,Len('{0}'))='{0}') ", Me.gv1.DataKeys(row.RowIndex).Item("Code").ToString())
            'sql.AppendFormat(";Delete from PLV_SFReport{0} ", DateTime.Today.ToString("yyyyMM"))
            'sql.AppendFormat("where SapCode in(select SapCode from ProductData where Left(ProdHierarchy,Len('{0}'))='{0}') ", Me.gv1.DataKeys(row.RowIndex).Item("Code").ToString())

            'sql.AppendFormat(";Delete from SFReport{0} ", DateTime.Today.AddMonths(1).ToString("yyyyMM"))
            'sql.AppendFormat("where SapCode in(select SapCode from ProductData where Left(ProdHierarchy,Len('{0}'))='{0}') ", Me.gv1.DataKeys(row.RowIndex).Item("Code").ToString())
            'sql.AppendFormat(";Delete from SO_SFReport{0} ", DateTime.Today.AddMonths(1).ToString("yyyyMM"))
            'sql.AppendFormat("where SapCode in(select SapCode from ProductData where Left(ProdHierarchy,Len('{0}'))='{0}') ", Me.gv1.DataKeys(row.RowIndex).Item("Code").ToString())
            'sql.AppendFormat(";Delete from PLV_SFReport{0} ", DateTime.Today.AddMonths(1).ToString("yyyyMM"))
            'sql.AppendFormat("where SapCode in(select SapCode from ProductData where Left(ProdHierarchy,Len('{0}'))='{0}') ", Me.gv1.DataKeys(row.RowIndex).Item("Code").ToString())

            'sql.AppendFormat(";Update ProductData set Active='N',MMPP='07' where Left(ProdHierarchy,Len('{0}'))='{0}' ", Me.gv1.DataKeys(row.RowIndex).Item("Code").ToString())
            'sql.AppendFormat(";exec BuildProductHierarchy ")
            '==========================================================================================

            'MyObj.WriteLog(sql.ToString())
            dat.AccData(sql.ToString())

            Me.ReBindData(Me.gv1.DataKeys(row.RowIndex).Item("LevelName").ToString())
        End If
    End Sub

#End Region

#Region "DummyProduct"
    Protected Sub gv2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv2.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If Me.gv2.DataKeys(e.Row.RowIndex).Item("IsDummy").ToString() = "N" Then
                TryCast(e.Row.FindControl("btnEdit"), Button).Visible = False
                TryCast(e.Row.FindControl("btnDelete"), Button).Visible = False
            End If
        End If
    End Sub

    Protected Sub gv2_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv2.PageIndexChanging
        Me.gv2.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv2_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv2.RowCommand
        If e.CommandName = "EditDummyProduct" Then
            Me.AccessMode = "Update"
            Me.SetProdLevel()

            Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
            Me.gv2.SelectedIndex = row.RowIndex

            Me.txtSapCode.Text = MyObj.GetAreaCode() & Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString()
            Me.txtSapCode.Enabled = False
            Me.txtProdHierarchy.Enabled = False
            Me.txtProdDescription.Text = Me.gv2.DataKeys(row.RowIndex).Item("Description").ToString()
            Me.txtLocalDescription.Text = Me.gv2.DataKeys(row.RowIndex).Item("LocalDescription").ToString()
            MyObj.MatchItem(Me.ddlMMPP, Me.gv2.DataKeys(row.RowIndex).Item("MaterialType").ToString())
            Me.txtPAllow.Text = Me.gv2.DataKeys(row.RowIndex).Item("PAllow").ToString()
            Me.txtListP.Text = Me.gv2.DataKeys(row.RowIndex).Item("ListP").ToString()
            Me.txtSTD.Text = Me.gv2.DataKeys(row.RowIndex).Item("STD").ToString()
            Me.mvEmployee.SetActiveView(vwAddDummyProduct)
        ElseIf e.CommandName = "DeleteDummyProduct" Then
            Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
            Dim sql As New MyStringBuilder
            Dim dat As New clsData

            '刪除Dummy Product時將ProductData的資料的Active設為N,MMPP設為07,刪除目前月份的SFReport版本的資料
            sql.Length = 0
            sql.AppendFormat("update ProductData set Active='N',MMPP='07' where SapCode='{0}'", Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())
            sql.AppendFormat(";Delete from SFReport{0} where SapCode='{1}' ", DateTime.Today.ToString("yyyyMM"), Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())
            sql.AppendFormat(";Delete from SO_SFReport{0} where SapCode='{1}' ", DateTime.Today.ToString("yyyyMM"), Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())
            sql.AppendFormat(";Delete from PLV_SFReport{0} where SapCode='{1}' ", DateTime.Today.ToString("yyyyMM"), Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())
            sql.AppendFormat(";Delete from WF_SFReport{0} where SapCode='{1}' ", DateTime.Today.ToString("yyyyMM"), Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())
            sql.AppendFormat(";exec BuildAllTable '{0}','{1}' ", DateTime.Today.AddMonths(1).ToString("yyyy"), DateTime.Today.AddMonths(1).ToString("MM"))
            sql.AppendFormat(";Delete from SFReport{0} where SapCode='{1}' ", DateTime.Today.AddMonths(1).ToString("yyyyMM"), Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())
            sql.AppendFormat(";Delete from SO_SFReport{0} where SapCode='{1}' ", DateTime.Today.AddMonths(1).ToString("yyyyMM"), Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())
            sql.AppendFormat(";Delete from PLV_SFReport{0} where SapCode='{1}' ", DateTime.Today.AddMonths(1).ToString("yyyyMM"), Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())
            sql.AppendFormat(";Delete from WF_SFReport{0} where SapCode='{1}' ", DateTime.Today.AddMonths(1).ToString("yyyyMM"), Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())
            dat.AccData(sql.ToString())

            Me.BindGV()
        End If
    End Sub
#End Region

#End Region

#Region "頁面事件"
    '搜尋
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Me.BindGV()
    End Sub

#Region "下拉式選單"
    Protected Sub ddlDivision_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDivision.SelectedIndexChanged
        Me.ReBindData("Signature")
    End Sub

    Protected Sub ddlSignature_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSignature.SelectedIndexChanged
        Me.ReBindData("Brand")
    End Sub

    Protected Sub ddlBrand_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBrand.SelectedIndexChanged
        Me.ReBindData("Sub-Brand")
    End Sub

    Protected Sub ddlSub_Brand_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSub_Brand.SelectedIndexChanged
        Me.ReBindData("Reference")
    End Sub

    Protected Sub ddlReference_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlReference.SelectedIndexChanged
        Me.ReBindData("Nuance")
    End Sub

    Protected Sub ddlNuance_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlNuance.SelectedIndexChanged
        Me.ReBindData("Axe")
    End Sub

    Protected Sub ddlAxe_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlAxe.SelectedIndexChanged
        Me.ReBindData("Sub-Axe")
    End Sub

    Protected Sub ddlSub_Axe_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSub_Axe.SelectedIndexChanged
        Me.ReBindData("Class")
    End Sub

    Protected Sub ddlClass_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlClass.SelectedIndexChanged
        Me.ReBindData("Function")
    End Sub

    Protected Sub ddlFunction_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlFunction.SelectedIndexChanged
        Me.BindGV()
    End Sub
#End Region

#Region "DummyHierarchy"
    '新增DummyHierarchy
    Protected Sub btnAddDummyHierarchy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddDummyHierarchy.Click
        Me.AccessMode = "Insert"
        Me.SetLevel()
        Me.txtProductHierarchy.Enabled = True
        Me.txtProductHierarchy.Text = ""
        Me.txtDescription.Text = ""
        Me.mvEmployee.SetActiveView(vwAddDummyHierarchy)
    End Sub

    '取消管理DummyHierarchy
    Protected Sub btnCancelDummyHierarchy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelDummyHierarchy.Click
        Me.mvEmployee.SetActiveView(vwSearch)
        Me.BindGV()
    End Sub

    '儲存DummyHierarchy
    Protected Sub btnSaveDummyHierarchy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveDummyHierarchy.Click
        If Me.txtProductHierarchy.Text.Trim().Length < 2 Then
            MyObj.ShowMessage(Me.GetLocalResourceObject("DataLength"))
            Return
        End If

        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        Dim MyLevelName As String = Me.lblLevelName.Text
        Dim MyParentCode As String = Me.lblParentCode.Text
        Dim MyProductHierarchy As String = Me.txtProductHierarchy.Text.Replace("'", "''").Trim()
        MyProductHierarchy = MyParentCode & MyProductHierarchy
        Dim MyDescription As String = Me.txtDescription.Text.Replace("'", "''").Trim()

        '若為獨立階層,需檢查是否已存在該Hierarchy
        If Me.AccessMode = "Insert" Then
            Dim hmanager As New HierarchyManager
            Dim dt As DataTable = hmanager.GetLevelInfo(MyLevelName)
            If dt.Rows.Count > 0 AndAlso dt.Rows(0)("Chain").ToString() = "N" Then
                sql.Length = 0
                sql.AppendFormat("Select top 1 * From ProductHierarchyOriginal ")
                sql.AppendFormat("Where 1=1 ")
                sql.AppendFormat("and Right(Code,2)='{0}' ", Right(MyProductHierarchy, 2))
                sql.AppendFormat("and LevelName='{0}' ", MyLevelName)
                If dat.GetData(sql.ToString()).Rows.Count > 0 Then
                    MyObj.ShowMessage(Me.GetLocalResourceObject("SameData"))
                    Return
                End If
            End If
        End If

        sql.Length = 0
        sql.AppendFormat("Select * From ProductHierarchyOriginal ")
        sql.AppendFormat("Where 1=1 ")
        sql.AppendFormat("and ((Code='{0}' ", MyProductHierarchy)
        sql.AppendFormat(") or (Description='{0}') ", MyDescription)
        sql.AppendFormat(")")
        sql.AppendFormat("and LevelName='{0}' ", MyLevelName)
        If Me.AccessMode = "Update" Then
            sql.AppendFormat("and ID<>'{0}' ", Me.SelectedID)
        End If

        '有重複資料則不新增
        If dat.GetData(sql.ToString()).Rows.Count > 0 Then
            MyObj.ShowMessage(Me.GetLocalResourceObject("SameData"))
            Return
        Else
            If Me.AccessMode = "Insert" Then
                sql.Length = 0
                sql.AppendFormat("Insert ProductHierarchyOriginal(Code,Description,LevelName,IsDummy,CreateBySystem) ")
                sql.AppendFormat("select '{0}' ", MyProductHierarchy)
                sql.AppendFormat(",'{0}' ", MyDescription)
                sql.AppendFormat(",'{0}' ", MyLevelName)
                sql.AppendFormat(",'{0}' ", "Y")
                sql.AppendFormat(",'{0}' ", "N")
                sql.AppendFormat(";exec BuildProductHierarchy ")
                dat.AccData(sql.ToString())
            ElseIf Me.AccessMode = "Update" Then
                sql.Length = 0
                sql.AppendFormat("update ProductHierarchyOriginal ")
                sql.AppendFormat("set ")
                sql.AppendFormat("Description='{0}' ", MyDescription)
                sql.AppendFormat("where ID='{0}' ", Me.SelectedID)
                sql.AppendFormat(";exec BuildProductHierarchy ")
                dat.AccData(sql.ToString())
            End If
            Me.mvEmployee.SetActiveView(vwSearch)
            Me.ReBindData(Me.lblLevelName.Text)
        End If
    End Sub
#End Region

#Region "DummyProduct"
    '新增DummyProduct
    Protected Sub btnAddDummyProduct_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddDummyProduct.Click
        Me.AccessMode = "Insert"
        Me.SetProdLevel()
        Me.txtSapCode.Enabled = False
        Me.txtSapCode.Text = "(Auto number)"
        Me.txtProdDescription.Text = ""
        Me.txtLocalDescription.Text = ""
        Me.mvEmployee.SetActiveView(vwAddDummyProduct)

        'MyObj.WriteLog(pmanager.NewDummyCode(Me.ddlSignature.Text))
    End Sub

    '取消管理DummyProduct
    Protected Sub btnCancelDummyProduct_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelDummyProduct.Click
        Me.mvEmployee.SetActiveView(vwSearch)
        Me.BindGV()
    End Sub

    '儲存DummyProduct
    Protected Sub btnSaveDummyProduct_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveDummyProduct.Click
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        'Dim MySapCode As String = Me.txtSapCode.Text.Replace("'", "''").Trim()
        Dim MySapCode As String
        If Me.AccessMode = "Insert" Then
            MySapCode = Me.pmanager.NewDummyCode(Me.ddlSignature.Text)
        Else
            MySapCode = Me.txtSapCode.Text.Replace("'", "''").Trim().Replace(MyObj.GetAreaCode(), "")
        End If
        Dim MyProductHierarchy As String = Me.txtProdHierarchy.Text.Replace("'", "''").Trim()
        Dim MyDescription As String = Me.txtProdDescription.Text.Replace("'", "''").Trim()
        Dim MyLocalDescription As String = Me.txtLocalDescription.Text.Replace("'", "''").Trim()
        Dim intValue As Integer
        Dim MyPAllow As String = Me.txtPAllow.Text.Replace("'", "''").Trim()
        If Not Integer.TryParse(MyPAllow, intValue) Then
            MyPAllow = "null"
        Else
            MyPAllow = MyPAllow * 0.01
        End If

        Dim MyListP As String = Me.txtListP.Text.Replace("'", "''").Trim()
        If Not IsNumeric(MyListP) Then
            MyListP = "null"
        Else
            MyListP = Math.Round(Convert.ToDecimal(MyListP), 2).ToString()
        End If

        Dim MySTD As String = Me.txtSTD.Text.Replace("'", "''").Trim()
        If Not IsNumeric(MySTD) Then
            MySTD = "null"
        Else
            MySTD = Math.Round(Convert.ToDecimal(MySTD), 2).ToString()
        End If

        sql.Length = 0
        sql.AppendFormat("Select * From ProductData ")
        sql.AppendFormat("Where 1=1 ")
        sql.AppendFormat("and SapCode='{0}' ", MySapCode)
        If Me.AccessMode = "Update" Then
            '    sql.AppendFormat("and ID<>'{0}' ", Me.SelectedID)
            sql.AppendFormat("and 1=2 ")
        End If

        '有重複資料則不新增
        If dat.GetData(sql.ToString()).Rows.Count > 0 Then
            MyObj.ShowMessage(Me.GetLocalResourceObject("SameSapCode"))
            Return
        Else
            If Me.AccessMode = "Insert" Then
                sql.Length = 0
                sql.AppendFormat("Insert ProductData(SapCode,ProdHierarchy,Description,LocalDescription,MMPP,MaterialType,IsDummy,Division) ")
                sql.AppendFormat("select '{0}' ", MySapCode)
                sql.AppendFormat(",'{0}' ", MyProductHierarchy)
                sql.AppendFormat(",'{0}' ", MyDescription)
                sql.AppendFormat(",'{0}' ", MyLocalDescription)
                sql.AppendFormat(",'{0}' ", "03") '預設為03,活的產品
                sql.AppendFormat(",'{0}' ", Me.ddlMMPP.SelectedValue)
                sql.AppendFormat(",'{0}' ", "Y")
                sql.AppendFormat(",'{0}' ", Me.ddlDivision.SelectedValue)
                sql.AppendFormat(";exec BuildDummyHierarchy '{0}' ", MyProductHierarchy)
                'sql.AppendFormat(";exec BuildSFReportBySapCode '{0}','{1}','{2}' ", DateTime.Today.ToString("yyyyMM"), "S", MySapCode)
                sql.AppendFormat(";exec BuildSFReportBySapCode '{0}','{1}','{2}' ", DateTime.Today.ToString("yyyyMM"), "M", MySapCode)
                'SellIn 有Generated,則全部一起建立
                Dim vManager As New VersionManager(DateTime.Today.ToString("yyyyMM"), Left(MyProductHierarchy, 2))
                If vManager.Generated = True Then
                    'sql.AppendFormat(";exec BuildSFReportBySapCode '{0}','{1}','{2}' ", DateTime.Today.AddMonths(1).ToString("yyyyMM"), "S", MySapCode)
                    sql.AppendFormat(";exec BuildSFReportBySapCode '{0}','{1}','{2}' ", DateTime.Today.AddMonths(1).ToString("yyyyMM"), "M", MySapCode)
                End If
                dat.AccData(sql.ToString())
            ElseIf Me.AccessMode = "Update" Then
                sql.Length = 0
                sql.AppendFormat("update ProductData ")
                sql.AppendFormat("set ")
                sql.AppendFormat("Description='{0}' ", MyDescription)
                sql.AppendFormat(",LocalDescription='{0}' ", MyLocalDescription)
                sql.AppendFormat(",MMPP='{0}' ", "03") '預設為03,活的產品
                sql.AppendFormat(",MaterialType='{0}' ", Me.ddlMMPP.SelectedValue)
                sql.AppendFormat("where SapCode='{0}' ", MySapCode)
                sql.AppendFormat(";exec BuildDummyHierarchy '{0}' ", MyProductHierarchy)
                dat.AccData(sql.ToString())
            End If
            sql.Length = 0
            sql.AppendFormat("exec SetProductDataBySapCode '{0}' ", MySapCode)
            dat.AccData(sql.ToString())

            '紀錄PAllow,ListP,STD
            sql.Length = 0
            sql.AppendFormat("Insert into ProductDummy(SapCode,PAllow,ListP,STD) ")
            sql.AppendFormat("select '{0}' ", MySapCode)
            sql.AppendFormat(",{0} ", MyPAllow)
            sql.AppendFormat(",{0} ", MyListP)
            sql.AppendFormat(",{0} ", MySTD)
            sql.AppendFormat("where not exists ")
            sql.AppendFormat("(Select top 1 * from ProductDummy where SapCode='{0}') ", MySapCode)
            sql.AppendFormat(";Update ProductDummy ")
            sql.AppendFormat("set PAllow={0} ", MyPAllow)
            sql.AppendFormat(",ListP={0} ", MyListP)
            sql.AppendFormat(",STD={0} ", MySTD)
            sql.AppendFormat("where SapCode='{0}' ", MySapCode)
            dat.AccData(sql.ToString())

            'Auto Fill
            If Me.chkAutoFill.Checked Then
                Dim monList As New List(Of String)
                For i As Integer = DateTime.Now.Month To 36
                    monList.Add(i)
                Next
                sql.Length = 0
                For i As Integer = 0 To 1
                    sql.AppendFormat("Update SFReport{0} ", DateTime.Now.AddMonths(i).ToString("yyyyMM"))
                    sql.AppendFormat("set {0} ", MyObj.JoinFormat(",", "Mon{0}=" & MyPAllow, monList.ToArray()))
                    sql.AppendFormat("where SapCode='{0}' ", MySapCode)
                    sql.AppendFormat("and SkuType='{0}' ", "PAllow")
                    sql.AppendFormat(";Update SFReport{0} ", DateTime.Now.AddMonths(i).ToString("yyyyMM"))
                    sql.AppendFormat("set {0} ", MyObj.JoinFormat(",", "Mon{0}=" & MyListP, monList.ToArray()))
                    sql.AppendFormat("where SapCode='{0}' ", MySapCode)
                    sql.AppendFormat("and SkuType='{0}' ", "ListP")
                    sql.AppendFormat(";Update SFReport{0} ", DateTime.Now.AddMonths(i).ToString("yyyyMM"))
                    sql.AppendFormat("set {0} ", MyObj.JoinFormat(",", "Mon{0}=" & MySTD, monList.ToArray()))
                    sql.AppendFormat("where SapCode='{0}' ", MySapCode)
                    sql.AppendFormat("and SkuType='{0}' ", "STD")
                Next
                dat.AccData(sql.ToString())
            Else
                sql.Length = 0
                sql.AppendFormat("Update SFReport{0} ", DateTime.Now.ToString("yyyyMM"))
                sql.AppendFormat("set Mon{0}={1} ", DateTime.Now.Month, MyPAllow)
                sql.AppendFormat("where SapCode='{0}' ", MySapCode)
                sql.AppendFormat("and SkuType='{0}' ", "PAllow")
                sql.AppendFormat(";Update SFReport{0} ", DateTime.Now.ToString("yyyyMM"))
                sql.AppendFormat("set Mon{0}={1} ", DateTime.Now.Month, MyListP)
                sql.AppendFormat("where SapCode='{0}' ", MySapCode)
                sql.AppendFormat("and SkuType='{0}' ", "ListP")
                sql.AppendFormat(";Update SFReport{0} ", DateTime.Now.ToString("yyyyMM"))
                sql.AppendFormat("set Mon{0}={1} ", DateTime.Now.Month, MySTD)
                sql.AppendFormat("where SapCode='{0}' ", MySapCode)
                sql.AppendFormat("and SkuType='{0}' ", "STD")
                dat.AccData(sql.ToString())
            End If

            Me.mvEmployee.SetActiveView(vwSearch)
            Me.BindGV()
        End If
    End Sub
#End Region

#End Region

#Region "繫結資料"
    Private Sub BindDDLDivision()
        Dim ddlControl As DropDownList = Me.ddlDivision
        Dim dvManager As New DivisionManager

        ddlControl.DataTextField = "Division"
        ddlControl.DataValueField = "Division"
        ddlControl.DataSource = dvManager.GetAllDivision()
        ddlControl.DataBind()
    End Sub

    Private Sub ReBindData(ByVal MyLevelName As String)
        Select Case MyLevelName
            Case "Signature"
                Me.BindDDLControlA(Me.ddlSignature, "Signature", "")
                Me.BindDDLControlC(Me.ddlBrand)
                Me.BindDDLControlC(Me.ddlSub_Brand)
                Me.BindDDLControlC(Me.ddlReference)
                Me.BindDDLControlC(Me.ddlNuance)
                Me.BindDDLControlC(Me.ddlAxe)
                Me.BindDDLControlC(Me.ddlSub_Axe)
                Me.BindDDLControlC(Me.ddlClass)
                Me.BindDDLControlC(Me.ddlFunction)
            Case "Brand"
                Me.BindDDLControlA(Me.ddlBrand, "Brand", Me.ddlSignature.SelectedValue)
                Me.BindDDLControlC(Me.ddlSub_Brand)
                Me.BindDDLControlC(Me.ddlReference)
                Me.BindDDLControlC(Me.ddlNuance)
                Me.BindDDLControlC(Me.ddlAxe)
                Me.BindDDLControlC(Me.ddlSub_Axe)
                Me.BindDDLControlC(Me.ddlClass)
                Me.BindDDLControlC(Me.ddlFunction)
            Case "Sub-Brand"
                Me.BindDDLControlA(Me.ddlSub_Brand, "Sub-Brand", Me.ddlBrand.SelectedValue)
                Me.BindDDLControlC(Me.ddlReference)
                Me.BindDDLControlC(Me.ddlNuance)
                Me.BindDDLControlC(Me.ddlAxe)
                Me.BindDDLControlC(Me.ddlSub_Axe)
                Me.BindDDLControlC(Me.ddlClass)
                Me.BindDDLControlC(Me.ddlFunction)
            Case "Reference"
                Me.BindDDLControlA(Me.ddlReference, "Reference", Me.ddlSub_Brand.SelectedValue)
                Me.BindDDLControlC(Me.ddlNuance)
                Me.BindDDLControlC(Me.ddlAxe)
                Me.BindDDLControlC(Me.ddlSub_Axe)
                Me.BindDDLControlC(Me.ddlClass)
                Me.BindDDLControlC(Me.ddlFunction)
            Case "Nuance"
                Me.BindDDLControlA(Me.ddlNuance, "Nuance", Me.ddlReference.SelectedValue)
                Me.BindDDLControlC(Me.ddlAxe)
                Me.BindDDLControlC(Me.ddlSub_Axe)
                Me.BindDDLControlC(Me.ddlClass)
                Me.BindDDLControlC(Me.ddlFunction)
            Case "Axe"
                Me.BindDDLControlB(Me.ddlAxe, "Axe", Me.ddlNuance.SelectedValue)
                Me.BindDDLControlC(Me.ddlSub_Axe)
                Me.BindDDLControlC(Me.ddlClass)
                Me.BindDDLControlC(Me.ddlFunction)
            Case "Sub-Axe"
                Me.BindDDLControlB(Me.ddlSub_Axe, "Sub-Axe", Me.ddlAxe.SelectedValue)
                Me.BindDDLControlC(Me.ddlClass)
                Me.BindDDLControlC(Me.ddlFunction)
            Case "Class"
                Me.BindDDLControlB(Me.ddlClass, "Class", Me.ddlSub_Axe.SelectedValue)
                Me.BindDDLControlC(Me.ddlFunction)
            Case "Function"
                Me.BindDDLControlB(Me.ddlFunction, "Function", Me.ddlClass.SelectedValue)
        End Select
        Me.BindGV()
    End Sub

    Private Sub BindDDLControlA(ByVal ddlControl As DropDownList, ByVal LevelName As String, ByVal ParentValue As String)
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.Length = 0
        sql.AppendFormat("Select distinct '('+Cast(Right(Code,2) as varchar)+')'+Description as NewDescription,Description,Code From ProductHierarchyOriginal ")
        sql.AppendFormat("Where LevelName = '{0}' ", LevelName)
        If LevelName <> "Signature" Then
            sql.AppendFormat("and Left(Code,Len(Code)-2) = '{0}' ", ParentValue)
        Else
            Dim stManager As New SignatureManager
            sql.AppendFormat("and Code IN ({0}) ", MyObj.JoinFormat(",", "'{0}'", stManager.GetSignatures(ddlDivision.SelectedValue)))
        End If
        sql.AppendFormat("and Active='Y' ")
        'sql.AppendFormat("order by Description ")
        sql.AppendFormat("order by NewDescription ")

        ddlControl.DataTextField = "NewDescription"
        ddlControl.DataValueField = "Code"
        ddlControl.DataSource = dat.GetData(sql)
        ddlControl.DataBind()
        ddlControl.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub

    Private Sub BindDDLControlB(ByVal ddlControl As DropDownList, ByVal LevelName As String, ByVal ParentValue As String)
        If ParentValue = "" Then
            Me.BindDDLControlC(ddlControl)
        Else
            Dim sql As New MyStringBuilder
            Dim dat As New clsData

            sql.Length = 0
            sql.AppendFormat("Select distinct '('+Cast(Right(Code,2) as varchar)+')'+Description as NewDescription,Description,Right(Code,2) as Code From ProductHierarchyOriginal ")
            sql.AppendFormat("Where LevelName = '{0}' ", LevelName)
            sql.AppendFormat("and Active='Y' ")
            sql.AppendFormat("order by NewDescription ")

            ddlControl.DataTextField = "NewDescription"
            ddlControl.DataValueField = "Code"
            ddlControl.DataSource = dat.GetData(sql)
            ddlControl.DataBind()
            ddlControl.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
        End If
    End Sub

    Private Sub BindDDLControlC(ByVal ddlControl As DropDownList)
        ddlControl.Items.Clear()
        ddlControl.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub

    Private Sub BindDDLMMPP(ByVal ddlControl As DropDownList)
        ddlControl.DataTextField = "Text"
        ddlControl.DataValueField = "Value"
        ddlControl.DataSource = MyObj.GetCodeMappingData("Product", "MPStatus")
        ddlControl.DataBind()
        ddlControl.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
        For i As Integer = ddlControl.Items.Count - 1 To 0 Step -1
            If ddlControl.Items(i).Value.IndexOf("All") <> -1 Then
                ddlControl.Items.RemoveAt(i)
            End If
        Next
    End Sub
#End Region

#Region "方法"
    Private Sub BindGV()
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.Length = 0

        Me.SetLevel()
        If Me.ddlFunction.SelectedValue <> "" Then
            Me.MyProdHierarchy = Me.lblParentCode.Text
            sql.AppendFormat("Select distinct a.*,a.ProdHierarchy as Code,b.Text as MMPP_Text ")
            sql.AppendFormat(",Case Isnull(pd.PAllow,'') when '' then '' else pd.PAllow*100 end as PAllow ")
            sql.AppendFormat(",pd.ListP,pd.STD ")
            sql.AppendFormat("From ProductData a ")
            sql.AppendFormat("left join CodeMapping b on a.MaterialType=b.Value and b.TableName='Product' and b.ColumnName='MPStatus' ")
            sql.AppendFormat("left join ProductDummy pd on a.SapCode=pd.SapCode ")
            sql.AppendFormat("Where a.ProdHierarchy='{0}' ", Me.MyProdHierarchy)
        Else
            sql.AppendFormat("Select distinct a.* From ProductHierarchyOriginal a ")
            sql.AppendFormat("Where 1=1 ")

            If Me.ddlSignature.SelectedValue = "" Then
                sql.AppendFormat("and LevelName = '{0}' ", "Signature")
                Dim stManager As New SignatureManager
                sql.AppendFormat("and Code IN ({0}) ", MyObj.JoinFormat(",", "'{0}'", stManager.GetSignatures(ddlDivision.SelectedValue)))
            Else
                sql.AppendFormat("and LevelName = '{0}' ", Me.lblLevelName.Text)
                sql.AppendFormat("and Left(Code,Len(Code)-2) = '{0}' ", Me.lblParentCode.Text)
            End If
        End If
        sql.AppendFormat("and a.Active='Y' ")
        'sql.AppendFormat("order by Description ")
        sql.AppendFormat("order by Code ")
        'MyObj.WriteLog(sql.ToString())
        If Me.ddlFunction.SelectedValue <> "" Then
            Me.gv2.DataSource = dat.GetData(sql)
            Me.gv2.DataBind()
            Me.tbDummyHierarchy.Visible = False
            Me.tbDummyProduct.Visible = True
        Else
            Me.gv1.DataSource = dat.GetData(sql)
            Me.gv1.DataBind()
            Me.tbDummyHierarchy.Visible = True
            Me.tbDummyProduct.Visible = False
        End If
    End Sub

    Private Sub SetLevel()
        Dim LevelName As String = ""
        Dim LevelDescription As String = ""
        Dim ParentCode As String = ""

        If Me.ddlSignature.SelectedValue = "" Then
            LevelName = "Signature"
            LevelDescription = "L'OREAL"
        Else
            LevelDescription = Me.ddlSignature.SelectedItem.Text
            ParentCode = Me.ddlSignature.SelectedValue
            If Me.ddlBrand.SelectedValue = "" Then
                LevelName = "Brand"
            Else
                LevelDescription &= "|" & Me.ddlBrand.SelectedItem.Text
                ParentCode = Me.ddlBrand.SelectedValue
                If Me.ddlSub_Brand.SelectedValue = "" Then
                    LevelName = "Sub-Brand"
                Else
                    LevelDescription &= "|" & Me.ddlSub_Brand.SelectedItem.Text
                    ParentCode = Me.ddlSub_Brand.SelectedValue
                    If Me.ddlReference.SelectedValue = "" Then
                        LevelName = "Reference"
                    Else
                        LevelDescription &= "|" & Me.ddlReference.SelectedItem.Text
                        ParentCode = Me.ddlReference.SelectedValue
                        If Me.ddlNuance.SelectedValue = "" Then
                            LevelName = "Nuance"
                        Else
                            LevelDescription &= "|" & Me.ddlNuance.SelectedItem.Text
                            ParentCode = Me.ddlNuance.SelectedValue
                            If Me.ddlAxe.SelectedValue = "" Then
                                LevelName = "Axe"
                            Else
                                LevelDescription &= "|" & Me.ddlAxe.SelectedItem.Text
                                ParentCode &= Me.ddlAxe.SelectedValue
                                If Me.ddlSub_Axe.SelectedValue = "" Then
                                    LevelName = "Sub-Axe"
                                Else
                                    LevelDescription &= "|" & Me.ddlSub_Axe.SelectedItem.Text
                                    ParentCode &= Me.ddlSub_Axe.SelectedValue
                                    If Me.ddlClass.SelectedValue = "" Then
                                        LevelName = "Class"
                                    Else
                                        LevelDescription &= "|" & Me.ddlClass.SelectedItem.Text
                                        ParentCode &= Me.ddlClass.SelectedValue
                                        If Me.ddlFunction.SelectedValue = "" Then
                                            LevelName = "Function"
                                        Else
                                            LevelDescription &= "|" & Me.ddlFunction.SelectedItem.Text
                                            ParentCode &= Me.ddlFunction.SelectedValue
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If

        Me.lblLevelName.Text = LevelName
        Me.lblLevelDescription.Text = LevelDescription
        Me.lblParentCode.Text = ParentCode
    End Sub

    Private Sub SetProdLevel()
        Dim LevelDescription As String = ""
        LevelDescription = Me.ddlSignature.SelectedItem.Text
        LevelDescription &= "|" & Me.ddlBrand.SelectedItem.Text
        LevelDescription &= "|" & Me.ddlSub_Brand.SelectedItem.Text
        LevelDescription &= "|" & Me.ddlReference.SelectedItem.Text
        LevelDescription &= "|" & Me.ddlNuance.SelectedItem.Text
        LevelDescription &= "|" & Me.ddlAxe.SelectedItem.Text
        LevelDescription &= "|" & Me.ddlSub_Axe.SelectedItem.Text
        LevelDescription &= "|" & Me.ddlClass.SelectedItem.Text
        LevelDescription &= "|" & Me.ddlFunction.SelectedItem.Text

        Me.lblProdLevelDescription.Text = LevelDescription
        Me.txtProdHierarchy.Text = Me.MyProdHierarchy
        Me.txtProdHierarchy.Enabled = False
        Me.ddlMMPP.SelectedIndex = -1
    End Sub
#End Region

#Region "屬性"
    Public Property SelectedID()
        Get
            Return ViewState("SelectedID")
        End Get
        Set(ByVal value)
            ViewState("SelectedID") = value
        End Set
    End Property

    Public Property LevelName()
        Get
            Return ViewState("LevelName")
        End Get
        Set(ByVal value)
            ViewState("LevelName") = value
        End Set
    End Property

    Public Property LevelDescription()
        Get
            Return ViewState("LevelDescription")
        End Get
        Set(ByVal value)
            ViewState("LevelDescription") = value
        End Set
    End Property

    Public Property ParentCode()
        Get
            Return ViewState("ParentCode")
        End Get
        Set(ByVal value)
            ViewState("ParentCode") = value
        End Set
    End Property

    Public Property AccessMode()
        Get
            Return ViewState("AccessMode")
        End Get
        Set(ByVal value)
            ViewState("AccessMode") = value
        End Set
    End Property

    Public Property MyProdHierarchy()
        Get
            Return ViewState("MyProdHierarchy")
        End Get
        Set(ByVal value)
            ViewState("MyProdHierarchy") = value
        End Set
    End Property
#End Region

End Class
